package com.qisimanxiang.dalaran.zuul;

import com.qisimanxiang.dalaran.zuul.config.ConsulConfig;
import com.qisimanxiang.dalaran.zuul.config.SentinelConfig;
import com.qisimanxiang.dalaran.zuul.filter.CustomRouteFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.EnableZuulServer;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Collections;
import java.util.List;

@SpringBootApplication
@EnableZuulProxy
@EnableScheduling
@EnableDiscoveryClient
@EnableConfigurationProperties({ConsulConfig.class, SentinelConfig.class})
public class DalaranZuulApplication {


    public static void main(String[] args) {
        SpringApplication.run(DalaranZuulApplication.class, args);
    }


    @SuppressWarnings("rawtypes")
    @Autowired(required = false)
    private List<RibbonRequestCustomizer> requestCustomizers = Collections.emptyList();

    @Bean
    public RibbonRoutingFilter ribbonRoutingFilter(ProxyRequestHelper helper,
                                                   RibbonCommandFactory<?> ribbonCommandFactory) {
        RibbonRoutingFilter filter = new CustomRouteFilter(helper, ribbonCommandFactory,
                this.requestCustomizers);
        return filter;
    }

}
